package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.model.SmsInfoDetailInfo;

import core.CoreDao;
import core.CoreService;
import core.db.DBConnectionUtil;

public class SmsInfoDetailDao {
	/**
	 * 新增短信详单信息
	 * 
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int insert(CoreDao dao, SmsInfoDetailInfo bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_sms_info_detail(session_id, app_key, cust_account,"
					+ "signcode,signature,template_code,"
					+ "msg_id,content,phone,"
					+ "status,status_desc,count,send_time,"
					+ "create_time,comments )");
			sql.append("VALUES(?,?,?,"
					+ "?,?,?,"
					+ "?,?,?,"
					+ "?,?,?,?"
					+ ",sysDate(),?)");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] {
					 bean.getSession_id(),bean.getApp_key(),bean.getCust_account(),
					 bean.getSigncode(),bean.getSignature(),bean.getTemplate_code(),
					 bean.getMsg_id(),bean.getContent(),bean.getPhone(),
					 bean.getStatus(),bean.getStatus_desc(),bean.getCount(),bean.getSend_time(),
					 bean.getComments()
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 新增短信详单信息
	 * 
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int update(CoreDao dao, SmsInfoDetailInfo bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"update t_sms_info_detail set status=?,status_desc=? where id=?");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] {
					 bean.getStatus(),bean.getStatus_desc(),bean.getId()
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 根据条件查询短信详单信息
	 * 
	 * @param dao
	 * @param session_id   短信唯一标识
	 * @param phone        手机号
	 * @param count        0表示整条，其余数字表示一条长短信被拆分的第几条
	 * @return
	 */
	public SmsInfoDetailInfo query(CoreDao dao, String session_id,String phone, String count) {
		Connection con = null;
		SmsInfoDetailInfo result = null;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("select * from t_sms_info_detail where session_id = ? and phone=? and count=?");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),	new Object[] {session_id,phone,count });

			if (list != null && list.size() > 0) {
				List<SmsInfoDetailInfo> items = dao.covertToBean(list,SmsInfoDetailInfo.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;

	}
	/**
	 * 查询条数（分页用）
	 * @param paramSql
	 * @return
	 */
	public int getTotalAcount(CoreDao dao, String paramSql) {
		if(StringUtils.isNotBlank(paramSql)){
			paramSql = " and " + paramSql;
		}
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select count(1) as totalAcount "
					+ " from t_sms_info_detail"
					+ " where 1=1  "
					+ paramSql );

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { });

			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalAcount");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 查询条数（分页用）
	 * @param paramSql
	 * @return
	 */
	public int getTotalUpstream(CoreDao dao, String paramSql) {
		if(StringUtils.isNotBlank(paramSql)){
			paramSql = " and " + paramSql;
		}
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select count(1) as totalAcount "
					+ " from t_sms_pushinfo"
					+ " where 1=1  "
					+ paramSql );

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { });

			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalAcount");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 查询数据（分页）
	 * @param paramSql
	 * @param pageSql
	 * @return
	 */
	public List<JSONObject> queryAll(CoreDao dao, String paramSql, String pageSql) {

		Connection con = null;
		List<JSONObject> result = null;
		if(StringUtils.isNotBlank(paramSql)){
			paramSql = " and " + paramSql;
		}

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select *  from t_sms_info_detail where 1=1"
					+ paramSql
					+ " order by send_time desc "
					+ pageSql );
			CoreService.log.info("短信发送结果查询--sql="+sql);
			result = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 查询数据（分页）
	 * @param paramSql
	 * @param pageSql
	 * @return
	 */
	public List<JSONObject> queryAllUpstreamList(CoreDao dao, String paramSql, String pageSql) {

		Connection con = null;
		List<JSONObject> result = null;
		if(StringUtils.isNotBlank(paramSql)){
			paramSql = " and " + paramSql;
		}

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select *  from t_sms_pushinfo where 1=1"
					+ paramSql
					+ " order by accepttime desc "
					+ pageSql );
			result = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
